package be.jprojects.subscription.controllers;

import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import be.jprojects.subscription.gui.listeners.CRUDActionListener;
import be.jprojects.subscription.gui.listeners.SelectedLanguageChangeListener;
import be.jprojects.subscription.gui.listeners.SelectedPersonChangeListener;
import be.jprojects.subscription.logic.interfaces.IPerson;


public class ListenersController {
	private static ListenersController instance;
	private Set<SelectedLanguageChangeListener> selectedLanguageChangeListeners;
	private Set<SelectedPersonChangeListener> selectedPersonChangeListeners;
	private Map<String, Set<CRUDActionListener> > crudChangeListeners;
	
	private ListenersController() {
		selectedLanguageChangeListeners = new HashSet<SelectedLanguageChangeListener>();
		selectedPersonChangeListeners = new HashSet<SelectedPersonChangeListener>();
		crudChangeListeners = new HashMap<String, Set<CRUDActionListener>>();
	}
	
	public void finalize() {
		selectedLanguageChangeListeners.clear();
		selectedPersonChangeListeners.clear();
		for (Map.Entry<String, Set<CRUDActionListener>> entry: crudChangeListeners.entrySet()) {
			entry.getValue().clear();
		}
		selectedLanguageChangeListeners = null;
		selectedPersonChangeListeners = null;
		crudChangeListeners = null;
	}

	public static ListenersController getInstance() {
		if (instance == null)
			instance = new ListenersController();
		return instance;
	}

	public void selectedPersonChanged(IPerson person) {
		for (SelectedPersonChangeListener spcl : selectedPersonChangeListeners) {
			spcl.selectedPersonChanged(person);
		}
	}
	
	public void selectedLanguageChanged() {
		for (SelectedLanguageChangeListener sll : selectedLanguageChangeListeners) {
			sll.selectedLanguageChanged();
		}
	}
	
	
	public void newActionPerformed(String name, ActionEvent e) {
		for (CRUDActionListener cal : crudChangeListeners.get(name)) {
			cal.newActionPerformed();
		}
	}
	public void updateActionPerformed(String name, ActionEvent e){
		for (CRUDActionListener cal : crudChangeListeners.get(name)) {
			cal.updateActionPerformed();
		}
	}
	public void deleteActionPerformed(String name,ActionEvent e){
		for (CRUDActionListener cal : crudChangeListeners.get(name)) {
			cal.deleteActionPerformed();
		}
	}
	public void saveActionPerformed(String name,ActionEvent e){
		for (CRUDActionListener cal : crudChangeListeners.get(name)) {
			cal.saveActionPerformed();
		}
	}
	public void cancelActionPerformed(String name,ActionEvent e){
		for (CRUDActionListener cal : crudChangeListeners.get(name)) {
			cal.cancelActionPerformed();
		}
	}
	
	
	public void addLanguageChangeListener(SelectedLanguageChangeListener sll) {
		selectedLanguageChangeListeners.add(sll);		
	}

	public void removeLanguageChangeListener(SelectedLanguageChangeListener sll) {
		selectedLanguageChangeListeners.remove(sll);		
	}
	
	public void addCRUDActionListener(String name, CRUDActionListener cal) {
		if (crudChangeListeners.get(name) == null) 
			crudChangeListeners.put(name, new HashSet<CRUDActionListener>());
		crudChangeListeners.get(name).add(cal);
	}
	
	public void removeCRUDActionListener(String name, CRUDActionListener cal) {
		crudChangeListeners.get(name).remove(cal);
		if (crudChangeListeners.get(name).size() == 0)
			crudChangeListeners.remove(name);
	}
	
	public void addSelectedPersonChangeListener(SelectedPersonChangeListener spcl) {
		selectedPersonChangeListeners.add(spcl);		
	}

	public void removeSelectedPersonChangeListener(SelectedPersonChangeListener spcl) {
		selectedPersonChangeListeners.remove(spcl);		
	}
	
	
}
